package com.malty.mall.controller.pms;

import com.malty.mall.common.CommonResult;
import com.malty.mall.domain.dto.req.pms.PmsAttributeParam;
import com.malty.mall.service.pms.IPmsProductAttributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author malty
 */
@RestController
@Api(tags = "商品属性管理")
@RequestMapping("/product/attribute")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class PmsProductAttributeController {
    private final IPmsProductAttributeService attributeService;

    @ApiOperation("创建属性")
    @PostMapping("/create")
    public CommonResult<Boolean> create(@RequestBody @Validated PmsAttributeParam cateParam) {
        return CommonResult.success(attributeService.create(cateParam));
    }

    @ApiOperation("编辑属性")
    @PostMapping("/update")
    public CommonResult<Boolean> update(@RequestBody @Validated PmsAttributeParam cateParam) {
        return CommonResult.success(attributeService.updateCategoryById(cateParam));
    }

    @GetMapping("/{id}")
    @ApiOperation("查询属性")
    public CommonResult<PmsAttributeParam> queryById(@PathVariable Integer id) {
        return CommonResult.success(attributeService.queryCateById(id));
    }

    @GetMapping("/list/{cid}")
    @ApiOperation("查询所有属性")
    public CommonResult<List<PmsAttributeParam>> list(@PathVariable Integer cid, Integer type, Integer pageNum, Integer pageSize) {
        return CommonResult.success(attributeService.list(cid, type, pageNum, pageSize));
    }

    @GetMapping("/attr_info/{id}")
    @ApiOperation("查询所有属性")
    public CommonResult<List<PmsAttributeParam>> list(@PathVariable Integer id) {
        return CommonResult.success(attributeService.listByProductCategoryId(id));
    }

    @ApiOperation("删除属性")
    @GetMapping("/delete/{id}")
    public CommonResult<Boolean> queryById(@PathVariable Long id) {
        return CommonResult.success(attributeService.deleteById(id));
    }
}
